//
//  DataViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/7.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class DataViewController: BaseViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {
    var tray : UIView?//navigation上面的额外视图
    var page : YXPageViewController?
    var currentIdx = 0
    var needMonthBtn : Bool = true
    var segment = UISegmentedControl.init(items: ["日","周","月"])
    var ctlArr : Array<UIViewController>?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        if needMonthBtn == true {
            self.title = "数据中心"
            configNavi()
            configPage()
        } else {
            self.title = "收入记录"
            configSeg()
            configPage()
            self.defaultBackButton()
        }
        self.naviHide = false
    }
    
    func monthData() -> Void {
        
    }
    
    @objc func segChange(seg : UISegmentedControl) -> Void {
        switch seg.selectedSegmentIndex {
        case 0:
            page?.setViewControllers([ctlArr![0]], direction: .reverse, animated: true, completion: nil)
            currentIdx = 0
            break
        case 1:
            if currentIdx > 1 {
                page?.setViewControllers([ctlArr![1]], direction: .reverse, animated: true, completion: nil)
            }else if currentIdx < 1 {
                page?.setViewControllers([ctlArr![1]], direction: .forward, animated: true, completion: nil)
            }
            currentIdx = 1
            break
        case 2:
            page?.setViewControllers([ctlArr![2]], direction: .forward, animated: true, completion: nil)
            currentIdx = 2
            break
        default:
            break
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func configPage()->Void{
        page = YXPageViewController.init(transitionStyle: .scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.horizontal, options: nil)
        ctlArr = []
        let subPage = DataPageViewController.init()
        subPage.index = 0
        subPage.alertCtl = self
        ctlArr?.append(subPage)
        let subPage1 = DataPageViewController.init()
        subPage1.index = 1
        subPage1.alertCtl = self
        ctlArr?.append(subPage1)
        let subPage2 = DataPageViewController.init()
        subPage2.index = 2
        subPage2.alertCtl = self
        ctlArr?.append(subPage2)
        page!.dataSource = self
        page!.delegate = self
        if needMonthBtn == true {
            page!.view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 91 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 44 - 91 * YXHeightScale))
        } else {
            page!.view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 91 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 91 * YXHeightScale))
            subPage.isHight = true
            subPage1.isHight = true
            subPage2.isHight = true
        }
        self.view.addSubview(page!.view)
        page!.setViewControllers([ctlArr![0]], direction: .forward, animated: true, completion: nil)
    }
    
    //pagectl代理方法
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if let idx = ctlArr!.index(of: viewController) {
            if idx > 0 {
                return ctlArr![idx - 1]
            }
        }
        return  nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if let idx = ctlArr?.index(of: viewController) {
            if idx < ctlArr!.count - 1 {
                return ctlArr![idx + 1]
            }
        }
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if let ctl = pageViewController.viewControllers {
            if completed {
                currentIdx = ctlArr!.index(of: ctl[0])!
                segment.selectedSegmentIndex = currentIdx
            }
        }
    }
    
    func configNavi() -> Void {
        self.rightButton(title: "月记录", target: self, selector: #selector(monthRecord))
        configSeg()
    }
    
    func configSeg() -> Void {
        self.addLine()
        tray = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: YXDeviceWidth, height: 91 * YXHeightScale)))
        tray!.backgroundColor = UIColor.white
        self.view.addSubview(tray!)
        segment.layer.borderColor = UIColor.colorWithHexString(hex: "2c2c2c").cgColor
        segment.layer.borderWidth = 2 * YXWidthScale
        segment.backgroundColor = UIColor.white
        segment.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 28 * YXWidthScale),NSAttributedStringKey.foregroundColor : UIColor.black], for: .normal)
        segment.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 28 * YXWidthScale),NSAttributedStringKey.foregroundColor : UIColor.white], for: .selected)
        segment.layer.cornerRadius = 8 * YXWidthScale
        segment.tintColor = UIColor.colorWithHexString(hex: "2c2c2c")
        segment.frame = CGRect.init(origin: CGPoint.init(x: 28 * YXWidthScale, y: 16 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth - 56 * YXWidthScale, height: 60 * YXHeightScale))
        tray!.addSubview(segment)
        segment.selectedSegmentIndex = 0
        segment.addTarget(self, action: #selector(segChange(seg:)), for: .valueChanged)
        let line = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: segment.bottom() + 16 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: YXHeightScale * 2)))
        line.backgroundColor = UIColor.colorWithHexString(hex: "ebebea")
        tray!.addSubview(line)
    }
    
    //月纪录按钮的点击事件
    @objc func monthRecord()->Void{
        let monthRecord = MonthRecorderViewController.init()
        monthRecord.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(monthRecord, animated: true)
    }
    
    deinit {
        print("dataCtl释放")
    }

}
